package com.itheima.mall.dubbo.api.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.dubbo.mapper.ums.ResourceMapper;
import com.itheima.mall.entity.domain.ums.Resource;
import com.itheima.mall.entity.domain.ums.RoleResourceRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 后台资源表 服务实现类
 * </p>
 *
 * @author qinglang
 * @since 2022-07-05
 */
@DubboService
public class ResourceImplService extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {


    @Autowired
    private RoleResourceRelationService roleResourceRelationService;
    @Override
    public void create(Resource resource) {
        this.save(resource);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        this.removeById(id);
        roleResourceRelationService.remove(new LambdaQueryWrapper<RoleResourceRelation>().eq(Objects.nonNull(id),RoleResourceRelation::getResourceId,id));
    }

    @Override
    public PageResult getList(Integer categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {
//        Integer pageNum = Integer.valueOf(map.get("pageNum").toString());
//        Integer pageSize = Integer.valueOf(map.get("pageSize").toString()) ;
        Page<Resource> resourcePage = new Page<>(pageNum,pageSize);
        Page<Resource> page = this.page(resourcePage,
                new LambdaQueryWrapper<Resource>()
                        .eq(Objects.nonNull(categoryId), Resource::getCategoryId,categoryId)
                        .like(Objects.nonNull(nameKeyword), Resource::getName,nameKeyword)
                        .like(Objects.nonNull(urlKeyword), Resource::getUrl,urlKeyword));
        return PageResult.init(page.getRecords(),page.getCurrent(),page.getSize(),page.getTotal());
    }

    @Override
    public List<Resource> listAll() {
        List<Resource> list = this.list();
        return list;
    }

    @Override
    public void updateRs(Resource resource) {
        this.updateById(resource);
    }

    @Override
    public Resource get(Integer id) {
       return this.getById(id);
    }
}
